using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Add, "PsSpecCmdletParameter", SupportsShouldProcess = true)]
    public class AddPsSpecCmdletParameterCommand : PsSpecCommandBase
    {
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 100)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = "*";

        [ValidateLength(1, 100)]
        [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
        public string CmdletName
        {
            get { return cmdletName; }
            set { cmdletName = value; }
        }
        private string cmdletName = null;

        [ValidateLength(1, 50)]
        [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
        private string projectName = null;

        [ValidateLength(1, 500)]
        [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        private string type = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        [AllowEmptyString]
        public string Description
        {
            get { return description; }
            set 
            {
                description = value; 
            }
        }
        private string description = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        [AllowEmptyCollection]
        public string[] Alias
        {
            get { return alias; }
            set { alias = value; }
        }
        private string[] alias = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool AllowGlobbing
        {
            get { return allowGlobbing; }
            set { allowGlobbing = value; }
        }
        private bool allowGlobbing = false;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool AllowMultiple
        {
            get { return allowMultiple; }
            set { allowMultiple = value; }
        }
        private bool allowMultiple = false;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("IsDynamic")]
        public bool Dynamic
        {
            get { return isDynamic; }
            set { isDynamic = value; }
        }
        private bool isDynamic = false;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool AllowEmptyCollection
        {
            get { return allowEmptyCollection; }
            set { allowEmptyCollection = value; }
        }
        private bool allowEmptyCollection = false;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool AllowEmptyString
        {
            get { return allowEmptyString; }
            set { allowEmptyString = value; }
        }
        private bool allowEmptyString = false;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool AllowNull
        {
            get { return allowNull; }
            set { allowNull = value; }
        }
        private bool allowNull = false;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        public int[] ValidateCount
        {
            get { return validateCount; }
            set
            {
                if ((value != null) && (value.Length != 2))
                    throw new ArgumentOutOfRangeException("ValidateCount");

                validateCount = value;
            }
        }
        private int[] validateCount = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool ValidateNotNull
        {
            get { return validateNotNull; }
            set { validateNotNull = value; }
        }
        private bool validateNotNull = true;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool ValidateNotNullOrEmpty
        {
            get { return validateNotNullOrEmpty; }
            set { validateNotNullOrEmpty = value; }
        }
        private bool validateNotNullOrEmpty = true;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        public int[] ValidateRange
        {
            get { return validateRange; }
            set
            {
                if ((value != null) && (value.Length != 2))
                    throw new ArgumentOutOfRangeException("ValidateRange");

                validateRange = value;
            }
        }
        private int[] validateRange = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        [AllowEmptyCollection]
        public string[] ValidateSet
        {
            get { return validateSet; }
            set { validateSet = value; }
        }
        private string[] validateSet = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        public int[] ValidateLength
        {
            get { return validateLength; }
            set
            {
                if ((value != null) && (value.Length != 2))
                    throw new ArgumentOutOfRangeException("ValidateLength");

                validateLength = value;
            }
        }
        private int[] validateLength = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        [AllowEmptyString]
        public string ValidatePattern
        {
            get { return validatePattern; }
            set
            {
                if ((value != null) && (value.Length > 200))
                    throw new ArgumentOutOfRangeException("ValidatePattern");

                validatePattern = value;
            }
        }
        private string validatePattern = null;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowNull]
        [AllowEmptyString]
        public string Notes
        {
            get { return notes; }
            set { notes = value; }
        }
        private string notes = null;


        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowEmptyString]
        public String DefaultValue
        {
            get
            {
                return defaultValue;
            }
            set
            {
                if ((value != null) && (value.Length > 200))
                    throw new ArgumentOutOfRangeException("DefaultValue");

                defaultValue = value;
            }
        }
        private string defaultValue;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowEmptyString]
        public String Custom1
        {
            get
            {
                return custom1;
            }
            set
            {
                custom1 = value;
            }
        }
        private string custom1;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowEmptyString]
        public String Custom2
        {
            get
            {
                return custom2;
            }
            set
            {
               custom2 = value;
            }
        }
        private string custom2;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowEmptyString]
        public String Custom3
        {
            get
            {
                return custom3;
            }
            set
            {
               custom3 = value;
            }
        }
        private string custom3;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowEmptyString]
        public String Custom4
        {
            get
            {
                return custom4;
            }
            set
            {
               custom4 = value;
            }
        }
        private string custom4;

        [Parameter(ValueFromPipelineByPropertyName = true)]
        [AllowEmptyString]
        public String HelpDescription
        {
            get
            {
                return helpDescription;
            }
            set
            {
               helpDescription = value;
            }
        }
        private string helpDescription;

        protected override void ProcessRecord()
        {
            // Check that they've specified a parameter name
            if (!PsSpecCmdletUtils.IsValidParameterName(Name))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.InvalidParameterName),
                    "INVALID_PARAMETER_NAME",
                    ErrorCategory.InvalidArgument, Name));
                return;
            }

            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.CreatePsSpecCmdletParameterShouldProcess, Name, CmdletName)))
            {                
                try
                {
                    Service.NewPsSpecCmdletParameter(ProjectName, CmdletName, Name, Type,
                        Description, PsSpecCmdletUtils.GetStringList(Alias),
                        AllowGlobbing,
                        AllowMultiple, Dynamic, AllowEmptyCollection, AllowEmptyString, AllowNull, PsSpecCmdletUtils.GetStringList(ValidateCount),
                        ValidateNotNull, ValidateNotNullOrEmpty, PsSpecCmdletUtils.GetStringList(ValidateRange), PsSpecCmdletUtils.GetStringList(ValidateSet),
                        PsSpecCmdletUtils.GetStringList(ValidateLength), ValidatePattern, Notes,
                        DefaultValue, Custom1, Custom2, Custom3, Custom4, HelpDescription);
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "CMDLET_SERVICE_ERROR",
                                                ErrorCategory.InvalidOperation,
                                                String.Format(resources.CreatePsSpecCmdletParameterShouldProcess, 
                                                    Name, CmdletName)));
                }
            }
        }
    }
}
